package com.ruoyi.tongban.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.tongban.androidapi.domain.AndroidSendChildSignMessage;
import com.ruoyi.tongban.androidapi.domain.AndroidSocketSession;
import com.ruoyi.tongban.androidapi.util.AndroidSessionCache;
import com.ruoyi.tongban.domain.*;
import com.ruoyi.tongban.mapper.*;
import com.ruoyi.tongban.service.IInsChildService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.ruoyi.tongban.service.IFaceTvDeviceService;

/**
 * 电视设备表Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-04-21
 */
@Service
public class FaceTvDeviceServiceImpl implements IFaceTvDeviceService 
{
    @Autowired
    private FaceTvDeviceMapper faceTvDeviceMapper;

    @Autowired
    private TInstitutionMapper tInstitutionMapper;

    @Autowired
    private InsGradeMapper insGradeMapper;

    @Autowired
    private InsClassMapper insClassMapper;

    @Autowired
    private IInsChildService childService;

    /**
     * 查询电视设备表
     * 
     * @param tvDeviceId 电视设备表ID
     * @return 电视设备表
     */
    @Override
    public FaceTvDevice selectFaceTvDeviceById(Long tvDeviceId)
    {
        return faceTvDeviceMapper.selectFaceTvDeviceById(tvDeviceId);
    }

    /**
     * 查询电视设备表列表
     * 
     * @param faceTvDevice 电视设备表
     * @return 电视设备表
     */
    @Override
    public List<FaceTvDevice> selectFaceTvDeviceList(FaceTvDevice faceTvDevice)
    {
        Long deptId = faceTvDevice.getInstitutionId();
        if(deptId != null){
            faceTvDevice.setTvDeptId(deptId);
        }
        
        Long gradeId = faceTvDevice.getGradeId();
        if(gradeId != null){
            faceTvDevice.setGradeId(gradeId);
        }

        Long classId = faceTvDevice.getClassId();
        if(classId != null){
            faceTvDevice.setTvClassId(classId);
        }
        return faceTvDeviceMapper.selectFaceTvDeviceList(faceTvDevice);
    }

    /**
     * 通过设备sn号查询列表
     * @param tvSn 设备sn
     * @return 电视表
     */
    @Override
    public FaceTvDevice selectFaceTvDeviceByTvSn(String tvSn) {
        return faceTvDeviceMapper.selectFaceTvDeviceByTvSn(tvSn);
    }

    /**
     * 新增电视设备表
     * 
     * @param faceTvDevice 电视设备表
     * @return 结果
     */
    @Override
    public int insertFaceTvDevice(FaceTvDevice faceTvDevice)
    {
        if(faceTvDevice.getTvSn() == null){
            throw new CustomException("添加失败,请先填写电视设备号");
        }
        String tvSn = faceTvDevice.getTvSn();
        FaceTvDevice result = faceTvDeviceMapper.selectFaceTvDeviceByTvSn(tvSn);
        if(result != null){
            throw new CustomException("添加失败,设备号已重号,请查询后重试");
        }
        Long institutionId = faceTvDevice.getInstitutionId();
        if(institutionId == null){
            throw new CustomException("添加失败,请选择机构名称");
        }
        TInstitution tInstitution = tInstitutionMapper.selectTInstitutionById(institutionId);
        faceTvDevice.setTvDeptId(tInstitution.getInstitutionId());
        faceTvDevice.setTvDeptName(tInstitution.getInstitutionName());
        Long gradeId = faceTvDevice.getGradeId();
        if(gradeId == null){
            throw new CustomException("添加失败,请选择年级名称");
        }
        InsGrade insGrade = insGradeMapper.selectInsGradeById(gradeId);
        faceTvDevice.setTvGradeId(insGrade.getGradeId());
        faceTvDevice.setTvGradeName(insGrade.getGradeName());
        faceTvDevice.setGradeId(gradeId);
        Long classId = faceTvDevice.getClassId();
        if(classId == null){
            throw new CustomException("添加失败,请选择班级名称");
        }
        InsClass insClass = insClassMapper.selectInsClassById(classId);
        faceTvDevice.setTvClassId(insClass.getClassId());
        faceTvDevice.setTvClassName(insClass.getClassName());
        faceTvDevice.setCreateTime(DateUtils.getNowDate());
        return faceTvDeviceMapper.insertFaceTvDevice(faceTvDevice);
    }

    /**
     * 修改电视设备表
     * 
     * @param faceTvDevice 电视设备表
     * @return 结果
     */
    @Override
    public int updateFaceTvDevice(FaceTvDevice faceTvDevice)
    {
        Long institutionId = faceTvDevice.getInstitutionId();
        if(institutionId == null){
            throw new CustomException("添加失败,请选择机构名称");
        }
        TInstitution tInstitution = tInstitutionMapper.selectTInstitutionById(institutionId);
        faceTvDevice.setTvDeptId(tInstitution.getInstitutionId());
        faceTvDevice.setTvDeptName(tInstitution.getInstitutionName());
        Long gradeId = faceTvDevice.getGradeId();
        if(gradeId == null){
            throw new CustomException("添加失败,请选择年级名称");
        }
        InsGrade insGrade = insGradeMapper.selectInsGradeById(gradeId);
        faceTvDevice.setTvGradeId(insGrade.getGradeId());
        faceTvDevice.setTvGradeName(insGrade.getGradeName());
        faceTvDevice.setGradeId(gradeId);
        Long classId = faceTvDevice.getClassId();
        if(classId == null){
            throw new CustomException("添加失败,请选择班级名称");
        }
        InsClass insClass = insClassMapper.selectInsClassById(classId);
        faceTvDevice.setTvClassId(insClass.getClassId());
        faceTvDevice.setTvClassName(insClass.getClassName());
        return faceTvDeviceMapper.updateFaceTvDevice(faceTvDevice);
    }

    /**
     * 批量删除电视设备表
     * 
     * @param tvDeviceIds 需要删除的电视设备表ID
     * @return 结果
     */
    @Override
    public int deleteFaceTvDeviceByIds(Long[] tvDeviceIds)
    {
        return faceTvDeviceMapper.deleteFaceTvDeviceByIds(tvDeviceIds);
    }

    /**
     * 删除电视设备表信息
     * 
     * @param tvDeviceId 电视设备表ID
     * @return 结果
     */
    @Override
    public int deleteFaceTvDeviceById(Long tvDeviceId)
    {
        return faceTvDeviceMapper.deleteFaceTvDeviceById(tvDeviceId);
    }
    
    public FaceTvDevice getClassInfoBySn(String sn){
        FaceTvDevice faceTvDevice = new FaceTvDevice();
        faceTvDevice.setTvSn(sn);//设备码有唯一索引
        List<FaceTvDevice> faceTvDeviceList = faceTvDeviceMapper.selectFaceTvDeviceList(faceTvDevice);
        if(faceTvDeviceList.size() == 0){
            return null;
        }
        return faceTvDeviceList.get(0);
    }
    
    public void insertDevice(String sn){
        FaceTvDevice faceTvDevice = getClassInfoBySn(sn);
        if(faceTvDevice == null){
            faceTvDevice = new FaceTvDevice();
            faceTvDevice.setTvSn(sn);
            faceTvDeviceMapper.insertFaceTvDevice(faceTvDevice);
        }
    }
    
    public List<InsChild> listAllChild(String sn){
        FaceTvDevice faceTvDevice = getClassInfoBySn(sn);
        if(faceTvDevice.getTvDeptId() == null){//没配置机构不允许拉数据
            return new ArrayList<InsChild>();
        }
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("institutionId", faceTvDevice.getTvDeptId());
        paramMap.put("gradeId", faceTvDevice.getTvGradeId());
        paramMap.put("classId", faceTvDevice.getTvClassId());
        return childService.selectChildSendTv(paramMap);
    }
    
    public Map<String,Object> listDeptChild(String sn){
        Map<String,Object> returnMap = new HashMap<String,Object>();
        FaceTvDevice faceTvDevice = getClassInfoBySn(sn);
        if(faceTvDevice == null){
            return returnMap;
        }
        if(faceTvDevice.getTvDeptId() == null){//没配置机构不允许拉数据
            return returnMap;
        }
        returnMap.put("deptName", faceTvDevice.getTvDeptName());
        InsClass clazz = new InsClass();
        clazz.setInstitutionId(faceTvDevice.getTvDeptId());
        List<InsClass> insClassList = insClassMapper.selectInsClassList(clazz);
        List<Map<String, Object>> returnList = new ArrayList<>();
        for(InsClass insClass : insClassList){
            Map<String,Object> tempMap = new HashMap<String,Object>();
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("institutionId", insClass.getInstitutionId());
            paramMap.put("gradeId", insClass.getGradeId());
            paramMap.put("classId", insClass.getClassId());
            tempMap.put("className", insClass.getClassName());
            tempMap.put("array", childService.selectChildSendTv(paramMap));
            returnList.add(tempMap);
        }
    
        returnMap.put("class", returnList);
        
        return returnMap;
    }
    
    public List<FaceTvDevice> selectFaceTvDeviceOfDeptId(FaceTvDevice faceTvDevice){
        return faceTvDeviceMapper.selectFaceTvDeviceOfDeptId(faceTvDevice);
    }
    
    /**
     * @Description 一直不在线,则10秒一次,
     * @Author caiyanshan
     * @Date 2021/4/21 15:50
     * @Param [sn]
     * @Return void
     */
    @Deprecated
    public void sendSignToTv(AndroidSendChildSignMessage androidSendChildSignMessage, FacePerson facePerson) {
        FaceTvDevice faceTvDevice = new FaceTvDevice();
        faceTvDevice.setTvClassId(facePerson.getClassId());
        faceTvDevice.setTvDeptId(facePerson.getDeptId());
        faceTvDevice.setTvGradeId(facePerson.getGradeId());
        List<FaceTvDevice> faceTvDeviceList = faceTvDeviceMapper.selectFaceTvDeviceList(faceTvDevice);
        if(faceTvDeviceList.size() == 0){
            return;
        }
        androidSendChildSignMessage.setCmd("sendChildrenSign");
        String sn = faceTvDeviceList.get(0).getTvSn();
        androidSendChildSignMessage.setSn(sn);
        Gson gson = new Gson();
        try{
            long i = 1;
            while(i != 20){
                AndroidSocketSession session = AndroidSessionCache.getSession(sn);
                if(session == null){//设备不在线
                    Thread.sleep(i * 1000);
                    i++;
                    continue;
                }
                session.getSession().sendText(gson.toJson(androidSendChildSignMessage));
                break;
            }
        }catch (Exception e){
        
        }
    }

}
